package com.lovo.util;

public class ArrayUtil {
	public static void main(String[] args){
//		String[] arr = {"1", "3", "4", "21"};
//		
//		arr = delet(arr, 1);
//		for (String string : arr) {
//			System.out.print(string + " ");
//		}
//		System.out.println();
		
		String arr[][] ={
				{"001", "张三", "5000"}, // 工号  姓名  工资
				{"002", "李四", "-3000"},
				{"003", "王五", "3800"},
				{"004", "小娟", "2500"},
				{"005", "小兰", "34000"},
		};
		//String st[] = {"006", "小明", "3000"};
//		arr = delet(arr, 2);
		sort(arr, false);
		
		for (String[] string : arr) {
			for (String string2 : string) {
				System.out.print(string2+"  ");
			}
			System.out.println();
		}
		System.out.println();
	}
	
//	排序 flag = true 为升序， 反之为降序
	public static void sort(int[] arr, boolean flag){
		for (int i = 0; i < arr.length; i++) {
			for (int j = i+1; j < arr.length; j++) {
				if(flag){
					if(arr[i] > arr[j]){
						int t = arr[i];
						arr[i] = arr[j];
						arr[j] = t;
						}
				}else{
					if(arr[i] < arr[j]){
						int t = arr[i];
						arr[i] = arr[j];
						arr[j] = t;
						}
				}
			}
		}
	}
	
	public static void sort(String[][] arr, boolean flag){
		for (int i = 0; i < arr.length; i++) {
			for (int j = i+1; j < arr.length; j++) {
				if(flag){
					if(Double.parseDouble(arr[i][2]) > Double.parseDouble(arr[j][2])){
						String[] t = arr[i];
						arr[i] = arr[j];
						arr[j] = t;
						}
				}else{
					if(Double.parseDouble(arr[i][2]) < Double.parseDouble(arr[j][2])){
						String[] t = arr[i];
						arr[i] = arr[j];
						arr[j] = t;
						}
				}
			}
		}
	}

//	增加元素到末尾
	public static String[][] add(String[][] arr, String[] string){
		String str[][] = new String[arr.length+1][];
		
		for (int i = 0; i < arr.length; i++) {
			str[i] = arr[i];
		}
		str[arr.length] = string;
		return str;
	}
	
//	增加元素到末尾
	public static String[] add(String[] arr, String string){
		String str[] = new String[arr.length+1];
		
		for (int i = 0; i < arr.length; i++) {
			str[i] = arr[i];
		}
		str[arr.length] = string;
		return str;
	}
 

//	去重
	public static String[] distinct(String[] arr){
		String[] newAr = new String[arr.length];
		int len = 0;
		for (int i = 0; i < newAr.length; i++) {
			boolean flag = false;
			for (int j = 0; j < len; j++) {
				if(arr[i] == newAr[j]){
					flag = true;
					break;
				}
			}
			if(!flag){
				newAr[len++] = arr[i];
				flag = false;
			}
		}
		
		String[] newAr2 = new String[len];
		for (int i = 0; i < newAr2.length; i++) {
			newAr2[i] = newAr[i];
		}
		
		return newAr2;
	}
	
//	删除指定位置的元素   pos： 指定元素位置，从1开始
	public static String[][] delet(String[][] arr, int pos){ 
		if(pos > arr.length || pos < 1){
			System.out.println("删除位置无效");
			return null;
		}
		String str[][] = new String[arr.length - 1][];
		
		int index = 0;
		while(index < arr.length - 1){
			if(index >= pos-1){
				str[index] = arr[index + 1];
			}else{
				str[index] = arr[index];
			}
			index++;
		}
		
		return str;
	}
	
//	删除指定位置的元素   pos： 指定元素位置，从1开始
	public static String[] delet(String[] arr, int pos){ 
		if(pos > arr.length || pos < 1){
			System.out.println("删除位置无效");
			return null;
		}
		String str[] = new String[arr.length - 1];
		
		int index = 0;
		while(index < arr.length - 1){
			if(index >= pos-1){
				str[index] = arr[index + 1];
			}else{
				str[index] = arr[index];
			}
			index++;
		}
		
		return str;
	}
}
